package cn.bdqn.exception;

import cn.bdqn.entity.MyEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

//全局异常处理器
@ControllerAdvice
public class ExceptionHandler {

    // 在@ExceptionHandler注解中指定异常类型
    @org.springframework.web.bind.annotation.ExceptionHandler
    @ResponseBody
    public MyEntity exceptionMapping(Exception exception) {// 方法形参位置接收SpringMVC捕获到的异常对象
        if(exception instanceof MyException){
            MyException e= (MyException) exception;
            System.out.println(e.getMessage());
            return MyEntity.fail(e.getMessage(),e.getCode(),e.getMessage());
        }else {
            return MyEntity.fail(exception.getMessage(),2000,exception.getMessage());
        }


    }
}